package com.core.system.controller;

import com.core.system.service.ISystemDictTypeService;
import com.core.system.utils.ConvertUtils;
import com.pojo.dto.SystemDictTypeDto;
import com.pojo.parent.PageResp;
import com.pojo.req.RemoveReq;
import com.pojo.req.dictType.DictTypeSaveReq;
import com.pojo.resp.dictType.DictTypePageResp;
import com.utility.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典类型表 前端控制器
 * @author Huawei
 * @date 2023-12-31 22:11
 */
@RestController
@RequestMapping("/api/system/dictType")
public class DictTypeController {

    @Autowired
    private ISystemDictTypeService dictTypeService;
    /**
     * 字典类型 分页
     */
    @GetMapping("/page")
    public Result<PageResp<SystemDictTypeDto>> page(@RequestParam Map<String,Object> paramMap) {
        return Result.returnCodeSuccess(dictTypeService.page(paramMap));
    }

    /**
     * 字典类型 分页
     *
     */
    @GetMapping("/list")
    public Result<List<DictTypePageResp>> list(@RequestParam Map<String,Object> paramMap) {
        return Result.returnCodeSuccess(dictTypeService.dictTypeList(paramMap));
    }

    /**
     * 刷新字典缓存
     * @param dictTypeCode 缓存code (非必填)
     * @return
     */
    @GetMapping("/refreshCache")
    public Result refreshCache(@RequestParam("dictTypeCode")String dictTypeCode){
        dictTypeService.refreshCache(dictTypeCode);
        return Result.returnCodeSuccess();
    }

    /**
     * 字典类型 inser
     */
    @PostMapping("/save")
    public Result<Object> save(@RequestBody @Validated DictTypeSaveReq dictTypeUpdateReq) {
        dictTypeService.saveOrUpdate(ConvertUtils.Convert(dictTypeUpdateReq, SystemDictTypeDto.class));
        return Result.returnCodeSuccess();
    }


    /**
     * 字典类型 inser
     */
    @PostMapping("/remove")
    public Result<Object> remove(@RequestBody @Validated RemoveReq removeReq) {
        dictTypeService.remove(removeReq.getId());
        return Result.returnCodeSuccess();
    }
}
